package com.zuihai.app.mvp.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.jess.arms.utils.KnifeUtil;
import com.zuihai.app.R;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/7/6 0006.
 */

public class MessageDialog extends Dialog {

    @BindView(R.id.tv_message)
    TextView mMessageView;

    @BindView(R.id.tv_title)
    TextView mTitleView;

    @BindView(R.id.tv_cancle)
    TextView mCancleView;

    @BindView(R.id.tv_confirm)
    TextView mConfirmView;

    private Build build;

    private MessageDialog(Build build) {
        this(build.context, R.style.centerDialog);
        this.build = build;
    }

    private MessageDialog(@NonNull Context context, @StyleRes int themeResId) {
        super(context, themeResId);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_message);
        KnifeUtil.bindTarget(this, this);
        initWindow();
        init();
    }

    private void init() {
        if (!TextUtils.isEmpty(build.msg)) {
            mMessageView.setText(build.msg);
        }
        if (!TextUtils.isEmpty(build.title)) {
            mTitleView.setText(build.title);
        }
        if (!TextUtils.isEmpty(build.cancelStr)) {
            mCancleView.setText(build.cancelStr);
        }
        if (!TextUtils.isEmpty(build.confrimStr)){
            mConfirmView.setText(build.confrimStr);
        }
    }

    /**
     * 初始化窗口信息
     */
    private void initWindow() {
        Window window = getWindow();
        //设置dialog在屏幕底部
        window.setGravity(Gravity.CENTER);
        //获得window窗口的属性
        WindowManager.LayoutParams lp = window.getAttributes();
        //设置窗口宽度为包裹内容
        lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
        //设置窗口高度为包裹内容
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        //将设置好的属性set回去
        window.setAttributes(lp);
        //设置dialog点击外部不消失
        setCanceledOnTouchOutside(false);
    }

    @OnClick(R.id.tv_cancle)
    public void onCancle(View view) {
        dismiss();
        if (build.cancelClickListener != null){
            build.cancelClickListener.cancelClick(this);
        }
    }

    @OnClick(R.id.tv_confirm)
    public void onConfirm(View v) {
        if (build.confrimClickListener != null){
            build.confrimClickListener.confrimClick(this);
        }
    }

    public static class Build {

        private String msg;
        private String title;
        private Context context;
        private String cancelStr;
        private String confrimStr;
        private OnConfrimClickListener confrimClickListener;
        private OnCancelClickListener cancelClickListener;

        public Build(Context context) {
            this.context = context;
        }

        public Build setMessage(String msg) {
            this.msg = msg;
            return this;
        }

        public Build setTitle(String title) {
            this.title = title;
            return this;
        }

        public Build setCancelStr(String cancelStr) {
            this.cancelStr = cancelStr;
            return this;
        }

        public Build setConfrimStr(String confrimStr) {
            this.confrimStr = confrimStr;
            return this;
        }

        public Build setConfrimClickListener(OnConfrimClickListener listener){
            this.confrimClickListener = listener;
            return this;
        }

        public Build setCancelClickListener(OnCancelClickListener listener){
            this.cancelClickListener = listener;
            return this;
        }

        public MessageDialog show() {
            MessageDialog dialog = new MessageDialog(this);
            dialog.show();
            return dialog;
        }
    }

    public interface OnConfrimClickListener{
        void confrimClick(MessageDialog dialog);
    }

    public interface OnCancelClickListener{
        void cancelClick(MessageDialog dialog);
    }
}
